package service

import (
	"fmt"
	"gameserver/gcommon"
	"gameserver/server/Linker/grpcmgr"
	"gameserver/server/Linker/internal"

	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/service"
)

var _ internal.IService = &Linker{}

type Linker struct {
	*service.Service

	GrpcSrv *grpcmgr.GrpcLinker
	//服务器信息
	Sinfo *gcommon.ServiceConfig
}

func NewService(sinfo *gcommon.ServiceConfig) (result *Linker) {
	result = &Linker{}
	result.Sinfo = sinfo
	result.Service = service.NewService(sinfo.GlobalConfig, result)
	v := sinfo.ListenMap[global.ListenType_Http]
	result.GrpcSrv = grpcmgr.NewGrpcNet(v, result.DisMgr)
	result.ListenMap[global.ListenType_Http] = fmt.Sprintf("%s:%d", v.Addr, result.GrpcSrv.Port)
	return
}

func (this *Linker) Init() (err error) {
	this.RegMsgProc(&Service_Proc{this})
	if err = this.Service.Init(); err != nil {
		return err
	}
	this.GrpcSrv.Start()
	return nil
}

func (this *Linker) Destroy() {
	this.GrpcSrv.Stop()
	this.Service.Destroy()
}
